package com.kungfuhacking.wristbandpro.my.presenter;

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;

import com.alibaba.fastjson.JSONObject;
import com.kungfuhacking.wristbandpro.base.bean.Basebean;
import com.kungfuhacking.wristbandpro.base.bean.RequestBean;
import com.kungfuhacking.wristbandpro.base.presenter.PhotoPresenter;
import com.kungfuhacking.wristbandpro.login.activity.LoginActivity;
import com.kungfuhacking.wristbandpro.my.view.ISettingView;
import com.kungfuhacking.wristbandpro.net.BaseNoTObserver;
import com.kungfuhacking.wristbandpro.net.RetrofitHttpUtil;
import com.kungfuhacking.wristbandpro.utils.CommonUtils;
import com.kungfuhacking.wristbandpro.utils.Constant;

import io.reactivex.disposables.Disposable;

/**
 * Created by Aaron on 17/5/12.
 */

public class ISettingPresenter extends PhotoPresenter {

    private ISettingView iSettingView;
    private String TAG = getClass().getSimpleName();

    public ISettingPresenter(ISettingView iSettingView) {
        this.iSettingView = iSettingView;
    }

    /**
     * 裁剪图片
     *
     * @param uri
     */
    public void startPhotoZoom(Uri uri) {
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(uri, "image/*");
        intent.putExtra("crop", "true");
        // aspectX aspectY 是宽高的比例
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        // outputX outputY 是裁剪图片宽高
        intent.putExtra("outputX", 300);
        intent.putExtra("outputY", 300);
        intent.putExtra("return-data", true);
        iSettingView.nextActivityForPhoto(intent, PHOTO_RESOULT);
    }

    /**
     * 获取七牛上传的token
     *
     * @param data
     */
    public void getToken(Intent data) {

        Bundle extras = data.getExtras();

        if (extras != null) {
            Bitmap photo = extras.getParcelable("data");

            getUploadToken(CommonUtils.compressByte(photo), iSettingView, new OnResultListener() {
                @Override
                public void onSuccess(String imgUrl) {
                    //图片上传成功
                    iSettingView.setImageUrl(imgUrl);
                    upDateUserInfo(iSettingView.getUsername(), iSettingView.getBirthaday(), iSettingView.getMySex(), imgUrl);
                }
            });
        }

    }

    public void setMyInfo() {
        iSettingView.setImageUrl(iSettingView.getSp().getString(Constant.USER_IMG, ""));
        iSettingView.setUsername(iSettingView.getSp().getString(Constant.NICKNAME, ""));
        iSettingView.setSex("女".equals(iSettingView.getSp().getString(Constant.SEX, "")) ? "女" : "男");
        iSettingView.setBirthday(iSettingView.getSp().getString(Constant.BIRTHDAY, ""));
        iSettingView.setTel(iSettingView.getSp().getString(Constant.MOBILE, ""));
    }

    /**
     * 更新用户数据
     *
     * @param nickName
     * @param birthday
     * @param sex
     * @param userImg
     */
    public void upDateUserInfo(final String nickName, final String birthday, final String sex, final String userImg) {

        JSONObject j = new JSONObject();
        j.put("token", iSettingView.getToken());
        j.put("nickName", nickName);
        j.put("birthDay", birthday);
        j.put("sex", sex);
        j.put("userImg", userImg);

        RetrofitHttpUtil.getInstance().updateUserInfo(new BaseNoTObserver<Basebean>() {
            @Override
            public void onHandleSuccess(Basebean basebean) {

                SharedPreferences.Editor edit = iSettingView.getSp().edit();
                edit.putString(Constant.NICKNAME, nickName);
                edit.putString(Constant.BIRTHDAY, birthday);
                edit.putString(Constant.SEX, sex);
                edit.putString(Constant.USER_IMG, userImg);
                edit.apply();
                setMyInfo();

            }

            @Override
            public void onHandleError(String message) {
                iSettingView.showErr(message);
            }

            @Override
            public void onReLogin(String message) {
                iSettingView.hideLoading();
                iSettingView.showErr(message);
                iSettingView.nextActivityWithSingleTask(LoginActivity.class);
            }

            @Override
            public void onSubscribe(Disposable d) {
                compositeDisposable.add(d);

            }
        }, new RequestBean().setData(j.toString()));
    }


    @Override
    public void clearRequest() {
        compositeDisposable.clear();
    }
}
